package com.camtc.camcenter.exception;

import com.camtc.camcenter.enums.CustomErrorInfo;
import com.camtc.camcenter.util.ExceptionUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 自定义异常
 *
 * @author
 * @date
 */

@Data
@Slf4j
public class CustomException extends RuntimeException {

    /**
     * 后台错误代码
     */
    protected final String errorCode;
    /**
     * 返回给前台的错误代码
     */
    private String code;

    /**
     * 无参构造-一般错误
     */
    public CustomException() {
        super(CustomErrorInfo.UNSPECIFIED.getDescription());
        this.errorCode = CustomErrorInfo.UNKNOW_ERROR.getCode();
    }

    /**
     * 错误信息描述
     *
     * @param description 错误信息
     */
    public CustomException(String description) {
        super(description);
        this.errorCode = CustomErrorInfo.UNKNOW_ERROR.getCode();
    }

    /**
     * 完整错误信息
     *
     * @param errorInfo
     */
    public CustomException(ErrorInfo errorInfo) {
        super(errorInfo.getDescription());
        this.errorCode = errorInfo.getCode();
    }

    /**
     * 指定异常构建返回异常
     *
     * @param t
     */
    public CustomException(Throwable t) {
        super(t);
        this.errorCode = CustomErrorInfo.UNKNOW_ERROR.getCode();
    }

    /**
     * 自定义错误信息处理
     *
     * @param errorInfo
     * @param description
     */
    public CustomException(CustomErrorInfo errorInfo, String... description) {
        super(ExceptionUtil.procExceptionInfo(errorInfo.getDescription(), description));
        this.errorCode = errorInfo.getCode();
    }
}
